package com.issac.fileupload.controller;

import org.apache.tomcat.util.http.fileupload.impl.FileSizeLimitExceededException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/*
* @ControllerAdvice
*   全局异常处理
*
* */

@ControllerAdvice
public class MyCustomException {
    /*
    //直接返回了文字
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public void myException(MaxUploadSizeExceededException e, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("上传文件大小超出限制");
        writer.flush();
        writer.close();
    }
    */
    //返回了界面
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ModelAndView myExceeption(MaxUploadSizeExceededException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("exceptionPage");
        modelAndView.addObject("error","俺限制了上传文件大小，你超过了");
        return modelAndView;
    }
}
